import re

import requests

word = input("输入>>>")
# 导入模块
url = 'https://tieba.baidu.com/f?ie=utf-8&kw=' + word
# 构造请求头
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                         'AppleWebKit/537.36 (KHTML, like Gecko) '
                         'Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43'
           }
# 　发送请求获取响应
response = requests.get(url, headers=headers)
html = response.text
limit_list = re.findall('<li class=" j_thread_list clearfix thread_item_box"(.*?)'
                        '<li class=', html, re.S)
title_list = {}
writer_list = {}
last_answer = {}
for i in range(0, len(limit_list)):
    title_list[i] = re.compile('class="j_th_tit ">(.*?)<').search(limit_list[i], re.S).group()
    writer_list[i] = re.compile('主题作者:(.*?)"').search(limit_list[i], re.S).group()
    last_answer[i] = re.compile('最后回复人:(.*?)"').search(limit_list[i]).group()
    print('title:' + title_list[i][18: -1])
    print(writer_list[i][: -1])
    print(last_answer[i][: -1] + "\n")
with open('getInfo.txt', 'r+', encoding='utf-8') as f:
    for i in range(0, len(title_list)):
        f.write('title:{}\n{}\n{}\n\n'.format(title_list[i][18: -1], writer_list[i][: -1], last_answer[i][: -1]))
